//
//  YCGoodsDetailViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/3/2.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCGoodsDetailViewController.h"
#define YCGoodsDetailCellIdentify @"YCGoodsDetailTableViewCell"
#define YCGoodDetail2CellIdentify @"YCGoodDetail2TableViewCell"
#define YCGoodDetail3CellIdentify @"YCGoodDetail3TableViewCell"

@interface YCGoodsDetailViewController ()
{
    UITableView *m_tableView;
    
    UIImageView *m_imgView;
    
    UIButton *m_btnSave;
    
    ItemTable *_itemTable;
    SDCycleScrollView *m_scrollView;
    BOOL _isCellSave;
    NSString *m_stock;
}
@end

@implementation YCGoodsDetailViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=_isStock ? @"编辑商品" : @"商品详情";
    self.navigationItem.leftBarButtonItem=[self tbarBackButton];
    
    [self addMainView];
    [self loadData];
}
-(void)loadData
{
    ItemDetailRequest *request = [ItemDetailRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    request.Id=_itemId;
    request.is_shop=[NSString stringWithFormat:@"%d",_isStock];
    [apiClient doItemDetail:request success:^(ApiResponse *data, NSString *url) {
        ItemDetailResponse *response=[[ItemDetailResponse alloc]initWithCGResponse:data];
        _itemTable=response.data;
        m_stock=_itemTable.stock;
        
        if (_isStock) {
            if (response.images.count>0) {
                [m_imgView load:response.images[0]];
            }            
        }else{
            if (response.images.count>1) {
                m_scrollView.infiniteLoop=YES;
                m_scrollView.autoScroll=YES;
            }else{
                m_scrollView.infiniteLoop=NO;
                m_scrollView.autoScroll=NO;
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                m_scrollView.imageURLStringsGroup = response.images;
            });
        }
        
        [m_tableView reloadData];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}
-(void)addMainView
{
    m_tableView=[[UITableView alloc]init];
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    m_tableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:m_tableView];
    if (_isStock) {
        [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(-60);
        }];
    }else{
        [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            make.top.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
        }];
    }
    
    [m_tableView registerNib:[UINib nibWithNibName:YCGoodsDetailCellIdentify bundle:nil] forCellReuseIdentifier:YCGoodsDetailCellIdentify];
    [m_tableView registerNib:[UINib nibWithNibName:YCGoodDetail2CellIdentify bundle:nil] forCellReuseIdentifier:YCGoodDetail2CellIdentify];
    [m_tableView registerNib:[UINib nibWithNibName:YCGoodDetail3CellIdentify bundle:nil] forCellReuseIdentifier:YCGoodDetail3CellIdentify];
    
    if (_isStock) {
        m_imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, W(self.view), kMainScreen_Width*3/4)];
        m_tableView.tableHeaderView=m_imgView;
        
        UIView *footerView=[[UIView alloc]init];
        [footerView setBackgroundColor:WHITE_COLOR];
        [self.view addSubview:footerView];
        [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(0);
            make.right.equalTo(self.view).offset(0);
            make.bottom.equalTo(self.view).offset(0);
            make.height.mas_equalTo(60);
        }];
        [footerView addShadowWith:CGSizeMake(W(self.view), 60)];
        
        m_btnSave=[[UIButton alloc]init];
        [m_btnSave setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        [m_btnSave setTitle:@"确认保存" forState:UIControlStateNormal];
        [m_btnSave.titleLabel setFont:[UIFont LightFontOfSize:16]];
        [m_btnSave setBackgroundColor:MAIN_COLOR];
        m_btnSave.layer.cornerRadius=2.0f;
        [m_btnSave addTarget:self action:@selector(buttonSaveClicked:) forControlEvents:UIControlEventTouchUpInside];
        [footerView addSubview:m_btnSave];
        [m_btnSave mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(footerView).offset(kDistance);
            make.right.equalTo(footerView).offset(-kDistance);
            make.top.equalTo(footerView).offset(kSmallDistance);
            make.bottom.equalTo(footerView).offset(-kSmallDistance);
        }];
    }else{
        m_scrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Width*3/4) imageURLStringsGroup:nil];
        m_scrollView.backgroundColor = WHITE_COLOR;
        m_tableView.tableHeaderView=m_scrollView;
        m_scrollView.showPageControl = YES;
        m_scrollView.autoScrollTimeInterval=4.0;
        m_scrollView.hidesForSinglePage=YES;
    }
}
-(void)buttonSaveClicked:(UIButton *)button
{
    _isCellSave=NO;
    [self loadDataUpdate];
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 5;
    }
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        CGFloat height=getTextSize(_itemTable.desc, kMainScreen_Width-80, [UIFont LightFontOfSize:15]).height+180-36;
        return height;
    }
    if (indexPath.section==1) {
        return 60;
    }
    if (indexPath.section==2) {
        CGFloat height=getTextSize(_itemTable.component, kMainScreen_Width-80, [UIFont LightFontOfSize:15]).height+140-36;
        return height;
    }
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section==0) {
        YCGoodsDetailTableViewCell *_cell = (YCGoodsDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoodsDetailCellIdentify forIndexPath:indexPath];
        _cell.isDesc=NO;
        [_cell bindWith:_itemTable];
        cell=_cell;
    }
    if (indexPath.section==1) {
        YCGoodDetail2TableViewCell *_cell = (YCGoodDetail2TableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoodDetail2CellIdentify forIndexPath:indexPath];
        [_cell.label1 setText:@"分类"];
        [_cell.labTitle setText:_itemTable.category];
        cell=_cell;
    }
    if (indexPath.section==2) {
        YCGoodsDetailTableViewCell *_cell = (YCGoodsDetailTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoodsDetailCellIdentify forIndexPath:indexPath];
        _cell.isDesc=YES;
        [_cell bindWith:_itemTable];
        cell=_cell;
    }
    if (indexPath.section==3) {
        if (_isStock) {
            YCGoodDetail3TableViewCell *_cell = (YCGoodDetail3TableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoodDetail3CellIdentify forIndexPath:indexPath];
            [_cell bindWith:_itemTable];
            _cell.saveBlock=^(NSString *num,BOOL isLoad){
                _isCellSave=YES;
                m_stock=num;
                if (isLoad) {
                    if (num.intValue==0) {
                        return;
                    }
                    [self loadDataUpdate];
                }
            };
            cell=_cell;
        }else{
            YCGoodDetail2TableViewCell *_cell = (YCGoodDetail2TableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCGoodDetail2CellIdentify forIndexPath:indexPath];
            _cell.labelWidthConstraint.constant=65;
            _cell.labTitleRightConstraint.constant=15;
            [_cell.label1 setText:@"可售库存"];
            [_cell.labTitle setText:_itemTable.num];
            [_cell.labTitle setTextColor:MAIN_COLOR];
            cell=_cell;
        }
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)loadDataUpdate
{
    if (m_stock.length==0) {
        [self.view showHUD:@"填写不能为空"];
        return;
    }
    ShopItemUpdateRequest *request = [ShopItemUpdateRequest new];
    request.item_id=_itemTable.Id;
    if (_isCellSave) {
        request.is_check=@"1";
    }
    request.num=m_stock;
    [apiClient hideProgress];
    [apiClient doShopItemUpdate:request success:^(ApiResponse *data, NSString *url) {
        [self.view showHUD:data.message];
        [self loadData];
        self.editBlock();
        if (!_isCellSave) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self goBack];
            });
        }
    }failure:^(ApiResponse *data, NSString *url){
    }];
}

@end
